using System;
using System.IO;

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Bcpg;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.IO;

namespace Flare.Tools.pgp_generator
{
	public class pgp_generator
	{
		public int _key_length {get; set;}
		public bool _armored {get;set;}
		public string _identity {get;set;}
		public string _passPhrase{get;set;}
		
		public pgp_generator(int key_length, bool armored, string identity, string passPhrase) // Encryption bytes: 1024, 2048, 3072, 4096
		{
			_key_length = key_length;
			_armored = armored;
			_identity = identity;
			_passPhrase = passPhrase;
		}
		
		#region ExportKeyPair
		public bool ExportKeyPair(
			Stream					SecretOut,
			Stream					PublicOut,
			AsymmetricKeyParameter	PrivateKey,
			AsymmetricKeyParameter	PublicKey,
			string					identity,
			char[]					passPhrase,
			bool					armored)
		{
			if(armored)
			{
				SecretOut = new ArmoredOutputStream(SecretOut);
			}
			
			PgpSecretKey SecretKey = new PgpSecretKey(
				PgpSignature.DefaultCertification,
				PublicKeyAlgorithmTag.RsaGeneral,
				PublicKey,
				PrivateKey,
				DateTime.UtcNow,
				identity,
				SymmetricKeyAlgorithmTag.Cast5,
				passPhrase,
				null,
				null,
				new SecureRandom()
				);
			
			SecretKey.Encode(SecretOut);
			
			if(armored)
			{
				SecretOut.Close();
				PublicOut = new ArmoredOutputStream(PublicOut);
			}
			
			PgpPublicKey pkey = SecretKey.PublicKey;
			
			pkey.Encode(PublicOut);
			
			if(armored)
			{
				PublicOut.Close();	
			}
			return true;
		}
		#endregion
	}
}

